package v1

import (
	"context"
	"fmt"
	"gitee.com/layheart/gameServer/global"
	"gitee.com/layheart/gameServer/model/common/request"
	"gitee.com/layheart/gameServer/model/common/response"
	message2 "gitee.com/layheart/gameServer/model/message"
	request2 "gitee.com/layheart/gameServer/model/message/request"
	response2 "gitee.com/layheart/gameServer/model/message/response"
	"go.uber.org/zap"
	"net"
)

type Message struct {
}

// List 列表
func (m *Message) List(ctx context.Context) error {
	conn := ctx.Value("conn").(net.Conn)
	//requestData := ctx.Value("params")

	// 查询数据库
	db := global.Mysql.Model(&message2.Message{})
	var messageList []message2.Message

	err := db.Scan(&messageList).Error
	if err != nil {
		fmt.Println("err:", err)
		return err
	}

	responseData := response2.MessageListRes{List: messageList}

	response.OkWithData(responseData, "success", conn)
	return nil
}

// Send 发送
func (m *Message) Send(ctx context.Context) error {
	conn := ctx.Value("conn").(net.Conn)
	requestData := ctx.Value("params")

	var params request2.MessageSendReq

	err := request.ShouldBindData(requestData, &params)
	if err != nil {
		global.Logger.Error("err:", zap.Error(err))
		return err
	}

	responseData := response2.MessageSendRes{
		Id:    1,
		Title: "我是标题",
		Desc:  "我是简介",
	}

	response.OkWithData(responseData, "success", conn)
	return nil
}
